package cn.lili.modules.member.mapper;


import cn.lili.modules.member.entity.dos.Member;
import cn.lili.modules.member.entity.vo.MemberStoreVO;
import cn.lili.modules.member.entity.vo.MemberVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 会员数据处理层
 *
 * @author Bulbasaur
 * @since 2020-02-25 14:10:16
 */
public interface MemberMapper extends BaseMapper<Member> {

    /**
     * 获取所有的会员手机号
     *
     * @return 会员手机号
     */
    @Select("select m.mobile from li_member m")
    List<String> getAllMemberMobile();

    @Select("select a.*,b.end_time MemberEndTime,m.level gradeName,w.member_wallet from li_member a" + " left join li_grade_level b on a.id = b.member_id " + " left join li_membership_card m on m.id = b.grade_id " + " left join li_member_wallet w on w.member_id = a.id ${ew.customSqlSegment}")
    IPage<MemberVO> pageByMemberVO(IPage<MemberVO> page, @Param(Constants.WRAPPER) Wrapper<Member> queryWrapper);

    @Select("SELECT a.*,b.end_time MemberEndTime,m.LEVEL gradeName,w1.member_wallet rechargeMoney ,w2.member_wallet promoteMoney,w3.member_wallet saleMoney" +
            " FROM li_member a" +
            " LEFT JOIN li_grade_level b ON a.id = b.member_id" +
            " LEFT JOIN li_membership_card m ON m.id = b.grade_id" +
            " LEFT JOIN li_member_wallet w1 ON w1.member_id = a.id and w1.owner = 'STORE_RECHARGE'" +
            " LEFT JOIN li_member_wallet w2 ON w2.member_id = a.id and w2.owner = 'STORE_PROMOTE'" +
            " LEFT JOIN li_member_wallet w3 ON w3.member_id = a.id and w3.owner = 'STORE_SALE'" +
            " ${ew.customSqlSegment}")
    IPage<MemberVO> pageByBMemberVO(IPage<MemberVO> page, @Param(Constants.WRAPPER) Wrapper<Member> queryWrapper);

    @Select("SELECT a.*,b.end_time MemberEndTime,m.LEVEL gradeName,w1.member_wallet rechargeMoney ,w2.member_wallet promoteMoney" +
            " FROM li_member a" +
            " LEFT JOIN li_grade_level b ON a.id = b.member_id" +
            " LEFT JOIN li_membership_card m ON m.id = b.grade_id" +
            " LEFT JOIN li_member_wallet w1 ON w1.member_id = a.id and w1.owner = 'BUYER_RECHARGE'" +
            " LEFT JOIN li_member_wallet w2 ON w2.member_id = a.id and w2.owner = 'BUYER_PROMOTE'" +
            " ${ew.customSqlSegment}")
    IPage<MemberVO> pageByCMemberVO(IPage<MemberVO> page, @Param(Constants.WRAPPER) Wrapper<Member> queryWrapper);

    @Select("SELECT count(e.id) quantity,m.*,p.id as partnerId," + "p.partner_type,p.partner_state,p.begin_time,p.end_time" + " from li_partner p" + " left join li_member m on p.member_id = m.id" + " left join li_extension e on e.extension = m.promotion_code ${ew.customSqlSegment}")
    IPage<MemberVO> updatePartner(IPage<MemberVO> page, @Param(Constants.WRAPPER) Wrapper<Member> queryWrapper);

    //获取用户信息
    @Select("select g.grade_id,c.level as gradeName,DATE_FORMAT(g.end_time,'%Y-%m-%d') as gradeEndTime,m.*,p.partner_type from li_member m " + "left join li_partner p on p.member_id = m.id and p.partner_state = 0 and p.delete_flag = 0 and NOW() < DATE_FORMAT(p.end_time,'%Y-%m-%d') " + "left join li_grade_level g on g.member_id = m.id and g.grade_state = 0 and g.delete_flag = 0 and NOW() < DATE_FORMAT(g.end_time,'%Y-%m-%d') " + "left join li_membership_card c on c.id = g.grade_id ${ew.customSqlSegment}")
    MemberVO findByUsername(@Param(Constants.WRAPPER) QueryWrapper<Member> queryWrapper);


    /**
     * 清空回收站
     */
    @Update("UPDATE li_member set delete_flag = 1 where disabled = 0")
    void cleanMember();


    @Select("select a.id,a.mobile,a.store_id,b.store_name,b.store_address_detail,b.member_id  from li_member a  left join li_store b on a.id = b.member_id ${ew.customSqlSegment}")
    IPage<MemberStoreVO> getmemSto(IPage<MemberStoreVO> page, @Param(Constants.WRAPPER) Wrapper<MemberStoreVO> queryWrapper);

}